package com.project.base.baseproject.AppCustoms.SelectImageActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.project.base.baseproject.AppCustoms.MyApplication.MyApplication;
import com.project.base.baseproject.AppCustoms.SelectImageActivity.BangBnagBucket;
import com.project.base.baseproject.R;
import com.project.base.baseproject.Utikles.DiviceInfo.DiviceInfo;
import com.squareup.picasso.Picasso;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.callback.BitmapCallback;

import java.util.ArrayList;

/**
 * Created by mac on 2017/5/25.
 */

public class MuliteSelectBucketAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private ArrayList<BangBnagBucket> mData;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    //初始化数据
    public MuliteSelectBucketAdapter(ArrayList<BangBnagBucket> arrayList) {
        this.mData = arrayList;
    }
    //更新数据
    public void updateData(ArrayList<BangBnagBucket> data) {
        this.mData = data;
        notifyDataSetChanged();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.mulite_select_bucket_item_layout, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        v.setOnClickListener(this);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder holder1 = (ViewHolder) holder;
        //取得地址
        final BangBnagBucket bangBnagBucket = this.mData.get(position);
        Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
        options.width = DiviceInfo.dpToPx(MyApplication.getInstance(),60);
        options.height = options.width;
        //options.height = xxx;//some compression configuration.
        Tiny.getInstance().source(bangBnagBucket.getThumbPath()).asBitmap().withOptions(options).compress(new BitmapCallback() {
            @Override
            public void callback(boolean isSuccess, Bitmap bitmap) {
                //return the compressed bitmap object
                holder1.imageView.setImageBitmap(bitmap);
            }
        });
        //这里要压缩 不然要OO
//        final Bitmap myBitmap = BitmapFactory.decodeFile(bangBnagBucket.getThumbPath());
//        BitmapUtils.scaleBitmap(myBitmap,new Handler(){
//            @Override
//            public void handleMessage(Message msg) {
//                super.handleMessage(msg);
//                holder1.imageView.setImageBitmap((Bitmap) msg.obj);
//            }
//        });
        holder1.bucketName.setText(bangBnagBucket.getBucketName());
        holder1.bucketNumber.setText(bangBnagBucket.getPhotoCount()+"");
        holder1.itemView.setTag(bangBnagBucket);
    }
    @Override
    public int getItemCount() {
        return this.mData.size();
    }

    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null)
            mOnItemClickListener.bucketClicked(view,(BangBnagBucket)view.getTag());
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView bucketName;
        TextView bucketNumber;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.bucket_image);
            bucketName = (TextView) itemView.findViewById(R.id.bucket_name);
            bucketNumber = (TextView) itemView.findViewById(R.id.bucket_number);
        }
    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void bucketClicked(View view,BangBnagBucket bangBnagBucket);
    }
}
